package com.atguigu.web;

import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import com.atguigu.service.impl.UserServiceImpl;
import com.atguigu.utils.webUtils;
import com.google.gson.Gson;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import com.google.code.kaptcha.*;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

/**
 * @author 14587
 * @date 2020/10/21 1:08
 */
public class UserServlet extends BaseServlet {


    private UserService userService=new UserServiceImpl();
    /**
     * @author VegetDog
     * @Description 用于登录
     * @date 2020-10-21 01:15
     * @param req
 * @param resp
     * @return void
     */
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user=new User(null,username,password,null);
        if (userService.login(user)!=null){
            HttpSession session = req.getSession();
            session.setAttribute("user",userService.login(user));
            session.setAttribute("username",username);
            req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req,resp);
        }else{
            req.setAttribute("msg","用户名或密码错误");
            req.setAttribute("username",username);
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
        }
    }
/**
 * @author VegetDog
 * @Description 注销信息
 * @date 2020-10-23 15:18
 * @param req
 * @param resp
 * @return void
 */
    protected void logOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //注销session
        req.getSession().invalidate();
        //重定向到首页
        resp.sendRedirect(req.getContextPath());
    }
    /**
     * @author VegetDog
     * @Description 用于注册
     * @date 2020-10-21 01:16
     * @param req
 * @param resp
     * @return void
     */
    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取谷歌包中的验证码
        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String code = req.getParameter("code");

        User user=webUtils.copyParamToBean(req.getParameterMap(),new User());
        /* System.out.println(username+password+email+code);*/
        //检查验证码是否正确
        if(token!=null&&token.equalsIgnoreCase(code)){
            if (!userService.existsUsername(username)) {
                userService.registUser(new User(null,username,password,email));
                req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req, resp);
            }else{
                req.setAttribute("msg","用户名已存在");
                req.setAttribute("username",username);
                req.setAttribute("email",email);
                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);
            }
        }else{
            req.setAttribute("msg","验证码错误");
            req.setAttribute("username",username);
            req.setAttribute("email",email);
            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);
        }

    }

    protected void ajaxExistUsername(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //1.获取请求的参数
        String username = req.getParameter("username");
        //2.调用userService.existsUsername();
        boolean isExists = userService.existsUsername(username);
        //3.把返回的对象封装成map键值对
        Map<String,Object> resultMap=new HashMap<String, Object>();
        resultMap.put("isExists",isExists);
        Gson gson=new Gson();
        String json = gson.toJson(resultMap);
        //通过响应的字符输出流输出
        resp.getWriter().write(json);
    }
}
